﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.Reflection;

namespace MacomberMapSystem.Common.User_Interfaces.Database
{
    /// <summary>
    /// This class presents a login option, including all information needed
    /// </summary>
    public class MM_Login_Option
    {
        #region Variable declarations
        /// <summary>
        /// This delegate handles the intended target for an option's logon code
        /// </summary>
        /// <param name="Selection">The selected value</param>
        /// <param name="ex">The exception from logging in</param>
        /// <returns></returns>
        public delegate bool LogonCodeDelegate(String Selection, out Exception ex);


        /// <summary>The caption for the combo box</summary>
        public String Caption;

        /// <summary>The list of options for this combo box</summary>
        public String[] Choices;

        /// <summary>The default value for this option</summary>
        public String Default = null;

        /// <summary>Whether a selection is required prior to connection</summary>
        public bool RequireSelection = false;

        /// <summary>The combo box associated with the choice</summary>
        public ComboBox AssociatedCombo;

        /// <summary>The procedure to invoke for logging on</summary>
        public LogonCodeDelegate LogonCode;
        #endregion

        #region Initialization
        /// <summary>
        /// Initialize a new login option
        /// </summary>
        /// <param name="Caption"></param>
        /// <param name="ToolTip"></param>
        /// <param name="Default"></param>
        /// <param name="RequireSelection"></param>
        /// <param name="ParentForm"></param>       
        /// <param name="tTip"></param>
        /// <param name="LogonCode"></param>
        /// <param name="Choices"></param>
        public MM_Login_Option(String Caption, String ToolTip, ToolTip tTip, String Default, bool RequireSelection, Form ParentForm, LogonCodeDelegate LogonCode, params string[] Choices)
        {
            //First, create our controls
            Label OutLabel = new Label();
            OutLabel.Text = Caption;            
            if (!String.IsNullOrEmpty(ToolTip))
                tTip.SetToolTip(OutLabel, ToolTip);

            //Create our new combo box, and add all items
            AssociatedCombo = new ComboBox();
            AssociatedCombo.DropDownStyle = ComboBoxStyle.DropDownList;
            if (!RequireSelection)
                AssociatedCombo.Items.Add("");
            AssociatedCombo.Items.AddRange(Choices);
            AssociatedCombo.Sorted = true;

            //Now, assign our default value if we have one
            if (!String.IsNullOrEmpty(Default))
                for (int a = 0; a < AssociatedCombo.Items.Count; a++)
                    if (AssociatedCombo.Items[a] as String == Default)
                        AssociatedCombo.SelectedIndex = a;

            this.Caption = Caption;
            this.Choices = Choices;
            this.Default = Default;
            this.RequireSelection = RequireSelection;
            this.LogonCode = LogonCode;            

            //Find our bottom combo box
            int MaxY = 0;
            foreach (Control ctl in ParentForm.Controls)
                if (ctl is ComboBox)
                    MaxY = Math.Max(ctl.Bottom, MaxY);

            AssociatedCombo.Location = new Point(110, MaxY + 9);
            AssociatedCombo.Size = new Size(254, 21);
            OutLabel.Location = new Point(27, MaxY + 12);
            OutLabel.Size = new Size(254, 21);
            ParentForm.Controls.Add(AssociatedCombo);
            ParentForm.Controls.Add(OutLabel);

            //Now, move our OK and Cancel buttons to a healthy distance
            ParentForm.Controls["btnOk"].Top = ParentForm.Controls["btnCancel"].Top= AssociatedCombo.Bottom + 21;
            ParentForm.ClientSize = new Size(ParentForm.ClientSize.Width, ParentForm.Controls["btnOk"].Bottom + 9);

        }
        #endregion



        /// <summary>
        /// Kick off the internal process to log on to the specified system
        /// </summary>
        /// <returns></returns>
        public bool LogOn(out Exception ex)
        {
            return LogonCode.Invoke(AssociatedCombo.Text, out ex);
        }
    }
}
